﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PicStore.Client
{
    public class ExcelHelper
    {
        public static DataTable ExcelToDT(string excelPath, string sheetName, out string date, out string error)
        {
            error = "";
            date = "";
            DataTable dt = new DataTable();
            string strConn = string.Format(ConfigurationManager.ConnectionStrings["ExcelConn"].ConnectionString,
                                           excelPath);
            using (OleDbConnection conn = new OleDbConnection(strConn))
            {
                try
                {
                    conn.Open();
                    string strExcel = "select * from [" + sheetName + "$]";
                    OleDbDataAdapter oda = new OleDbDataAdapter(strExcel, conn);
                    oda.Fill(dt);

                    date = dt.Columns[1].ColumnName;
                    if (date.IndexOf(":") > 0)
                    {
                        date = date.Substring(date.IndexOf(":") + 1).Trim();
                    }
                    else if (date.IndexOf("：") > 0)
                    {
                        date = date.Substring(date.IndexOf("：") + 1).Trim();
                    }
                }
                catch (Exception ex)
                {
                    //ApplicationLog.Error(ex.ToString());
                    error = ex.ToString();
                }
                finally
                {
                    conn.Close();
                }
            }
            return dt;
        }

        public static string GetStringValueOfValue(string value)
        {
            string result = string.Empty;
            if (value.Length > 1)
            {
                result = value.TrimStart(new char[] { '0' });
            }
            if (string.IsNullOrEmpty(result))
            {
                result = "0";
            }

            return result;
        }
    }
}
